/*
 * @Project Name: shoplook
 * @File Name: CustomerController
 * @Package Name: com.sm.shoplook.controller
 * @Date: 2018/8/14 10:32
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.controller.front;

import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.customer.Customer;
import com.sm.shoplook.service.customer.ApiCustomerService;
import com.sm.shoplook.util.FileUtil;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.customer.RegCustomerReqVo;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author chenpy
 * @desc 会员-前端接口
 * @date 2018/8/14 10:32
 * @see
 */
@RestController
@RequestMapping("/front/api/customer")
public class ApiCustomerController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiCustomerController.class);
	@Autowired
	private ApiCustomerService customerService;
	@Autowired
	private FileUtil fileUtil;

	@InitBinder
	public void init(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/21 17:13
	 * @Description 登录
	 * @param customer 会员信息
	 * @return
	 */
	@RequestMapping("/login")
	public ApiResult<String> login(Customer customer) {
		return customerService.login(customer);
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/21 17:13
	 * @Description 注册
	 * @param regCustomerReqVo 注册信息
	 * @param headImgFile 图像文件
	 * @param businessLicenseFile 营业执照
	 * @return
	 */
	@RequestMapping("/regCustomer")
	public ApiResult<String> regCustomer(RegCustomerReqVo regCustomerReqVo, MultipartFile headImgFile,
			MultipartFile businessLicenseFile, HttpServletRequest request) {
		try {
			if (regCustomerReqVo.getType() == 2) {
				Map<String, Object> objectMap = WebUtils.getParametersStartingWith(request, "business-");
				BeanUtils.populate(regCustomerReqVo, objectMap);
				if (businessLicenseFile != null) {
					String s = fileUtil.uploadFile(businessLicenseFile);
					regCustomerReqVo.setBusinessLicense(s);
				}
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String birthday = request.getParameter("b-birthday");
				String regTime = request.getParameter("b-regTime");
				regCustomerReqVo.setBirthday(simpleDateFormat.parse(birthday));
				regCustomerReqVo.setRegTime(simpleDateFormat.parse(regTime));
				regCustomerReqVo.setAuditStatus(1);
			}
			if (headImgFile != null) {
				String s = fileUtil.uploadFile(headImgFile);
				regCustomerReqVo.setHeadImg(s);
			}
			return customerService.insertCustomer(regCustomerReqVo);
		} catch (ApiException | IllegalAccessException | InvocationTargetException | ParseException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/21 17:13
	 * @Description 修改用户信息
	 * @param customer 会员信息
	 * @param headImgFile 图像文件
	 * @param businessLicenseFile 营业执照
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateCustomer")
	public ApiResult<String> updateCustomer(Customer customer, MultipartFile headImgFile,
			MultipartFile businessLicenseFile, HttpServletRequest request) {
		try {
			if (headImgFile != null) {
				String s = fileUtil.uploadFile(headImgFile);
				customer.setHeadImg(s);
			}
			if (businessLicenseFile != null) {
				String s = fileUtil.uploadFile(businessLicenseFile);
				customer.setBusinessLicense(s);
			}
			return customerService.updateCustomer(customer);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/getCustomerInfo", method = RequestMethod.GET)
	public ApiResult<Customer> getCustomerInfo(Integer id) {
		return customerService.selectCustomerById(id);
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/30 14:14
	 * @Description 修改密码
	 * @param id
	 * @param oldPassword 旧密码
	 * @param updNewPassword 新密码
	 * @param updConfirmPassword 确认信密码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	public ApiResult<Integer> updatePassword(Integer id, String oldPassword, String updNewPassword,
			String updConfirmPassword) {
		try {
			return customerService.updatePassword(id, oldPassword, updNewPassword, updConfirmPassword);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

    /**
     * 忘记密码
     * @param account
     * @param password
     * @param confirmPassword
     * @param authCode
     * @return
     */
	@ResponseBody
	@RequestMapping(value = "/updateForgetPassword", method = RequestMethod.POST)
	public ApiResult<Integer> updateForgetPassword(String account, String password, String confirmPassword,
			String authCode) {
		try {
			return customerService.updateForgetPassword(account, password, confirmPassword, authCode);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

    /**
     * 发送短信验证码
     * @param account
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sendSms", method = RequestMethod.POST)
    public ApiResult sendSms(String account, Integer type) {
        return customerService.sendSms(account, type);
    }
}
